package com.hyf.glidedemo.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PointF;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;
import com.hyf.glidedemo.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import jp.wasabeef.glide.transformations.BlurTransformation;
import jp.wasabeef.glide.transformations.ColorFilterTransformation;
import jp.wasabeef.glide.transformations.CropCircleTransformation;
import jp.wasabeef.glide.transformations.CropSquareTransformation;
import jp.wasabeef.glide.transformations.CropTransformation;
import jp.wasabeef.glide.transformations.GrayscaleTransformation;
import jp.wasabeef.glide.transformations.MaskTransformation;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
import jp.wasabeef.glide.transformations.gpu.BrightnessFilterTransformation;
import jp.wasabeef.glide.transformations.gpu.ContrastFilterTransformation;
import jp.wasabeef.glide.transformations.gpu.InvertFilterTransformation;
import jp.wasabeef.glide.transformations.gpu.KuwaharaFilterTransformation;
import jp.wasabeef.glide.transformations.gpu.PixelationFilterTransformation;
import jp.wasabeef.glide.transformations.gpu.SepiaFilterTransformation;
import jp.wasabeef.glide.transformations.gpu.SketchFilterTransformation;
import jp.wasabeef.glide.transformations.gpu.SwirlFilterTransformation;
import jp.wasabeef.glide.transformations.gpu.ToonFilterTransformation;
import jp.wasabeef.glide.transformations.gpu.VignetteFilterTransformation;

/**
 * Created by Administrator on 2017/12/18.
 */

public class GlideTransformationAdapter extends RecyclerView.Adapter<GlideTransformationAdapter.MyViewholder> {

    private Context mContext;
    private List<Integer> datas;

    public GlideTransformationAdapter(Context context, List<Integer> datas) {
        mContext = context;
        this.datas = datas;
    }

    @Override
    public GlideTransformationAdapter.MyViewholder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_glide_recycler_item, parent, false);
        return new MyViewholder(view);
    }

    @Override
    public void onBindViewHolder(GlideTransformationAdapter.MyViewholder holder, int position) {
        TextView textView = holder.mTextView;
        ImageView imageView = holder.mImageView;

        textView.setTextColor(Color.WHITE);
        switch (datas.get(position)) {
            case -1:
                // 原图
                textView.setText("原图");
                Glide.with(mContext)
                        .load(R.mipmap.qingxin)
                        .bitmapTransform(new CenterCrop(mContext))
                        .into(imageView);
                break;
            case 0:
                // 自定义mask
                textView.setText("自定义mask");
                Glide.with(mContext)
                        .load(R.mipmap.qingxin)
                        .override(500, 500)
                        .bitmapTransform(new CenterCrop(mContext), new MaskTransformation(mContext, R.mipmap.mask_starfish))
                        .into(imageView);
                break;
            case 1:
                // .9mask遮罩
                textView.setText(".9mask遮罩");
                Glide.with(mContext)
                        .load(R.mipmap.qingxin)
                        .override(500, 500)
                        .bitmapTransform(new CenterCrop(mContext), new MaskTransformation(mContext, R.mipmap.mask_chat_right))
                        .into(imageView);
                break;

            case 2:
                // 只显示顶部
                textView.setText("只显示顶部");
                Glide.with(mContext)
                        .load(R.mipmap.qingxin)
                        .bitmapTransform(new CropTransformation(mContext, 500, 200, CropTransformation.CropType.TOP))
                        .into(imageView);
                break;

            case 3:
                // 只显示顶部
                textView.setText("只显示中间");
                Glide.with(mContext)
                        .load(R.mipmap.qingxin)
                        .bitmapTransform(new CropTransformation(mContext, 500, 200, CropTransformation.CropType.CENTER))
                        .into(imageView);
                break;
            case 4:
                // 只显示顶部
                textView.setText("只显示底部");
                Glide.with(mContext)
                        .load(R.mipmap.qingxin)
                        .bitmapTransform(new CropTransformation(mContext, 500, 200, CropTransformation.CropType.BOTTOM))
                        .into(imageView);
                break;

            case 5:
                // 正方形显示
                textView.setText("正方形显示");
                Glide.with(mContext)
                        .load(R.mipmap.qingxin)
                        .bitmapTransform(new CropSquareTransformation(mContext))
                        .into(imageView);
                break;

            case 6:
                // 圆形显示
                textView.setText("圆形显示");
                Glide.with(mContext)
                        .load(R.mipmap.qingxin)
                        .bitmapTransform(new CropCircleTransformation(mContext))
                        .into(imageView);
                break;

            case 7:
                // 变换颜色
                textView.setText("变换颜色");
                Glide.with(mContext)
                        .load(R.mipmap.qingxin)
//                        .bitmapTransform(new ColorFilterTransformation(mContext,Color.argb(80, 255, 0, 0)))
                        .bitmapTransform(new ColorFilterTransformation(mContext, Color.parseColor("#60ff705e")),new CenterCrop(mContext))
                        .into(imageView);
                break;

            case 8:
                // 灰色滤镜
                textView.setText("灰色滤镜");
                Glide.with(mContext)
                        .load(R.mipmap.qingxin)
                        .bitmapTransform(new GrayscaleTransformation(mContext),new CenterCrop(mContext))
                        .into(imageView);
                break;

            case 9:
                // 灰色滤镜
                textView.setText("圆角效果");
                Glide.with(mContext)
                        .load(R.mipmap.qingxin)
                        .bitmapTransform(new RoundedCornersTransformation(mContext, 45, 0),new CenterCrop(mContext))
                        .into(imageView);
                break;

            case 10:
                // 高斯模糊
                textView.setText("高斯模糊");
                Glide.with(mContext)
                        .load(R.mipmap.qingxin)
                        // 说明 radius最大25   sampling为缩放级别
                        .bitmapTransform(new BlurTransformation(mContext, 24, 6),new CenterCrop(mContext))
                        .into(new GlideDrawableImageViewTarget(imageView));
                break;

            case 11:
                // Toon
                textView.setText("Toon(什么玩意儿)");
                Glide.with(mContext)
                        .load(R.mipmap.qingxin)
                        .bitmapTransform(new ToonFilterTransformation(mContext),new CenterCrop(mContext))
                        .into(imageView);
                break;

            case 12:
                // 棕色
                textView.setText("棕色");
                Glide.with(mContext)
                        .load(R.mipmap.qingxin)
                        .bitmapTransform(new SepiaFilterTransformation(mContext),new CenterCrop(mContext))
                        .into(imageView);
                break;

            case 13:
                // 对比度
                textView.setText("对比度");
                Glide.with(mContext)
                        .load(R.mipmap.qingxin)
//                        .bitmapTransform( new ContrastFilterTransformation(mContext,2.0f))
                        .bitmapTransform(new ContrastFilterTransformation(mContext, 1.0f),new CenterCrop(mContext))
                        .into(imageView);
                break;

            case 14:
                // 反色
                textView.setText("反色");
                Glide.with(mContext)
                        .load(R.mipmap.qingxin)
                        .bitmapTransform(new InvertFilterTransformation(mContext),new CenterCrop(mContext))
                        .into(imageView);
                break;

            case 15:
                // 像素
                textView.setText("像素");
                Glide.with(mContext)
                        .load(R.mipmap.qingxin)
                        // 默认1px
                        .bitmapTransform(new PixelationFilterTransformation(mContext,5f),new CenterCrop(mContext))
                        .into(imageView);
                break;

            case 16:
                // 素描
                textView.setText("素描");
                Glide.with(mContext)
                        .load(R.mipmap.qingxin)
                        .bitmapTransform(new SketchFilterTransformation(mContext),new CenterCrop(mContext))
                        .into(imageView);
                break;

            case 17:
                // 漩涡
                textView.setText("漩涡");
                Glide.with(mContext)
                        .load(R.mipmap.qingxin)
                        .bitmapTransform(new SwirlFilterTransformation(mContext,0.5f, 1.0f, new PointF(0.5f, 0.5f)),new CenterCrop(mContext))
                        .into(imageView);
                break;

            case 18:
                // 亮度
                textView.setText("亮度");
                Glide.with(mContext)
                        .load(R.mipmap.qingxin)
                        .bitmapTransform(new BrightnessFilterTransformation(mContext,0.2f),new CenterCrop(mContext))
                        .into(imageView);
                break;
                case 19:
                // 油画效果
                textView.setText("油画效果");
                Glide.with(mContext)
                        .load(R.mipmap.qingxin)
                        .bitmapTransform(new KuwaharaFilterTransformation(mContext,20),new CenterCrop(mContext))
                        .into(imageView);
                break;

            case 20:
                // Vignette
                textView.setText("Vignette");
                Glide.with(mContext)
                        .load(R.mipmap.qingxin)
                        .bitmapTransform(new VignetteFilterTransformation(mContext),new CenterCrop(mContext))
                        .into(imageView);
                break;
        }
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    class MyViewholder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_glide_recycler_item)
        TextView mTextView;
        @BindView(R.id.iv_glide_recycler_item)
        ImageView mImageView;

        public MyViewholder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
